<?php
class ControllerUserUser extends Controller {
    private $error = array();

    public function index() {
        $this->load->language('user/user');

        $this->document->title = $this->language->get('heading_title');

        $this->load->model('user/user');

        $this->getList();
    }

    public function insert() {
        $this->load->language('user/user');

        $this->document->title = $this->language->get('heading_title');

        $this->load->model('user/user');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_user_user->addUser($this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            $this->redirect(HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . $url);
        }

        $this->getForm();
    }

    public function update() {
        $this->load->language('user/user');

        $this->document->title = $this->language->get('heading_title');

        $this->load->model('user/user');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_user_user->editUser($this->request->get['user_id'], $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            $this->redirect(HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . $url);
        }

        $this->getForm();
    }

    public function delete() {
        $this->load->language('user/user');

        $this->document->title = $this->language->get('heading_title');

        $this->load->model('user/user');

        if (isset($this->request->post['selected']) && $this->validateDelete()) {
            foreach ($this->request->post['selected'] as $user_id) {
                $this->model_user_user->deleteUser($user_id);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            $this->redirect(HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . $url);
        }

        $this->getList();
    }

    private function getList() {
        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 'username';
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'ASC';
        }

        $url = '';

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $this->document->breadcrumbs = array();

        $this->document->breadcrumbs[] = array(
            'href' => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
            'text' => $this->language->get('text_home'),
            'separator' => FALSE
        );

        $this->document->breadcrumbs[] = array(
            'href' => HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . $url,
            'text' => $this->language->get('heading_title'),
            'separator' => ' :: '
        );

        $this->data['insert'] = HTTPS_SERVER . 'index.php?route=user/user/insert&token=' . $this->session->data['token'] . $url;
        $this->data['delete'] = HTTPS_SERVER . 'index.php?route=user/user/delete&token=' . $this->session->data['token'] . $url;

        $this->data['users'] = array();

        $data = array(
            'sort' => $sort,
            'order' => $order,
            'start' => ($page - 1) * $this->config->get('config_admin_limit'),
            'limit' => $this->config->get('config_admin_limit')
        );

        $user_total = $this->model_user_user->getTotalUsers();

        $results = $this->model_user_user->getUsers($data);

        foreach ($results as $result) {
            $action = array();

            $action[] = array(
                'text' => $this->language->get('text_edit'),
                'href' => HTTPS_SERVER . 'index.php?route=user/user/update&token=' . $this->session->data['token'] . '&user_id=' . $result['user_id'] . $url
            );

            $this->data['users'][] = array(
                'user_id' => $result['user_id'],
                'username' => $result['username'],
                'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
                'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
                'contract_download' => ($result['contract'] ? HTTPS_SERVER . 'index.php?route=user/user_contract&token=' . $this->session->data['token'] . '&user_id=' . $result['user_id'] : $this->language->get('text_disabled')),
                'contract' => $result['contract'],
                'selected' => isset($this->request->post['selected']) && in_array($result['user_id'], $this->request->post['selected']),
                'action' => $action
            );
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_no_results'] = $this->language->get('text_no_results');

        $this->data['column_username'] = $this->language->get('column_username');
        $this->data['column_status'] = $this->language->get('column_status');
        $this->data['column_date_added'] = $this->language->get('column_date_added');
        $this->data['column_action'] = $this->language->get('column_action');
        $this->data['column_download'] = $this->language->get('column_download');

        $this->data['button_insert'] = $this->language->get('button_insert');
        $this->data['button_delete'] = $this->language->get('button_delete');
        $this->data['button_dogovorPdf'] = $this->language->get('button_pdf_download');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $url = '';

        if ($order == 'ASC') {
            $url .= '&order=DESC';
        } else {
            $url .= '&order=ASC';
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['sort_username'] = HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . '&sort=username' . $url;
        $this->data['sort_status'] = HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . '&sort=status' . $url;
        $this->data['sort_date_added'] = HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . '&sort=date_added' . $url;

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $pagination = new Pagination();
        $pagination->total = $user_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_admin_limit');
        $pagination->text = $this->language->get('text_pagination');
        $pagination->url = HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . $url . '&page={page}';

        $this->data['pagination'] = $pagination->render();

        $this->data['sort'] = $sort;
        $this->data['order'] = $order;

        $this->template = 'user/user_list.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    }

    private function getForm() {
        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');
        $this->data['text_image_manager'] = $this->language->get('text_image_manager');

        $this->data['entry_username'] = $this->language->get('entry_username');
        $this->data['entry_password'] = $this->language->get('entry_password');
        $this->data['entry_confirm'] = $this->language->get('entry_confirm');
        $this->data['entry_firstname'] = $this->language->get('entry_firstname');
        $this->data['entry_lastname'] = $this->language->get('entry_lastname');
        $this->data['entry_email'] = $this->language->get('entry_email');
        $this->data['entry_company_name'] = $this->language->get('entry_company_name');
        $this->data['entry_opf'] = $this->language->get('entry_opf');
        $this->data['entry_okpo'] = $this->language->get('entry_okpo');
        $this->data['entry_okonx'] = $this->language->get('entry_okonx');
        $this->data['entry_inn'] = $this->language->get('entry_inn');
        $this->data['entry_certification'] = $this->language->get('entry_certification');
        $this->data['entry_licenses'] = $this->language->get('entry_licenses');
        $this->data['entry_user_group'] = $this->language->get('entry_user_group');
        $this->data['entry_status'] = $this->language->get('entry_status');
        $this->data['entry_contract'] = $this->language->get('entry_contract');
        $this->data['entry_captcha'] = $this->language->get('entry_captcha');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');
        $this->data['button_dogovor'] = $this->language->get('button_download');
        $this->data['button_dogovorPdf'] = $this->language->get('button_pdf_download');

        $this->data['tab_general'] = $this->language->get('tab_general');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['username'])) {
            $this->data['error_username'] = $this->error['username'];
        } else {
            $this->data['error_username'] = '';
        }

        if (isset($this->error['password'])) {
            $this->data['error_password'] = $this->error['password'];
        } else {
            $this->data['error_password'] = '';
        }

        if (isset($this->error['confirm'])) {
            $this->data['error_confirm'] = $this->error['confirm'];
        } else {
            $this->data['error_confirm'] = '';
        }

        if (isset($this->error['firstname'])) {
            $this->data['error_firstname'] = $this->error['firstname'];
        } else {
            $this->data['error_firstname'] = '';
        }

        if (isset($this->error['lastname'])) {
            $this->data['error_lastname'] = $this->error['lastname'];
        } else {
            $this->data['error_lastname'] = '';
        }

        $url = '';

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $this->document->breadcrumbs = array();

        $this->document->breadcrumbs[] = array(
            'href' => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
            'text' => $this->language->get('text_home'),
            'separator' => FALSE
        );

        $this->document->breadcrumbs[] = array(
            'href' => HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . $url,
            'text' => $this->language->get('heading_title'),
            'separator' => ' :: '
        );

        if (!isset($this->request->get['user_id'])) {
            $this->data['action'] = HTTPS_SERVER . 'index.php?route=user/user/insert&token=' . $this->session->data['token'] . $url;
        } else {
            $this->data['action'] = HTTPS_SERVER . 'index.php?route=user/user/update&token=' . $this->session->data['token'] . '&user_id=' . $this->request->get['user_id'] . $url;
        }

        $this->data['cancel'] = HTTPS_SERVER . 'index.php?route=user/user&token=' . $this->session->data['token'] . $url;

        if (isset($this->request->get['user_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $user_info = $this->model_user_user->getUser($this->request->get['user_id']);
        }

        if (isset($this->request->post['username'])) {
            $this->data['username'] = $this->request->post['username'];
        } elseif (isset($user_info)) {
            $this->data['username'] = $user_info['username'];
        } else {
            $this->data['username'] = '';
        }

        if (isset($this->request->post['password'])) {
            $this->data['password'] = $this->request->post['password'];
        } else {
            $this->data['password'] = '';
        }

        if (isset($this->request->post['confirm'])) {
            $this->data['confirm'] = $this->request->post['confirm'];
        } else {
            $this->data['confirm'] = '';
        }

        if (isset($this->request->post['firstname'])) {
            $this->data['firstname'] = $this->request->post['firstname'];
        } elseif (isset($user_info)) {
            $this->data['firstname'] = $user_info['firstname'];
        } else {
            $this->data['firstname'] = '';
        }

        if (isset($this->request->post['lastname'])) {
            $this->data['lastname'] = $this->request->post['lastname'];
        } elseif (isset($user_info)) {
            $this->data['lastname'] = $user_info['lastname'];
        } else {
            $this->data['lastname'] = '';
        }

        if (isset($this->request->post['email'])) {
            $this->data['email'] = $this->request->post['email'];
        } elseif (isset($user_info)) {
            $this->data['email'] = $user_info['email'];
        } else {
            $this->data['email'] = '';
        }

        if (isset($this->request->post['company_name'])) {
            $this->data['company_name'] = $this->request->post['company_name'];
        } elseif (isset($user_info)) {
            $this->data['company_name'] = $user_info['company_name'];
        } else {
            $this->data['company_name'] = '';
        }

        if (isset($this->request->post['opf'])) {
            $this->data['opf'] = $this->request->post['opf'];
        } elseif (isset($user_info)) {
            $this->data['opf'] = $user_info['opf'];
        } else {
            $this->data['opf'] = '';
        }

        if (isset($this->request->post['okpo'])) {
            $this->data['okpo'] = $this->request->post['okpo'];
        } elseif (isset($user_info)) {
            $this->data['okpo'] = $user_info['okpo'];
        } else {
            $this->data['okpo'] = '';
        }

        if (isset($this->request->post['okonx'])) {
            $this->data['okonx'] = $this->request->post['okonx'];
        } elseif (isset($user_info)) {
            $this->data['okonx'] = $user_info['okonx'];
        } else {
            $this->data['okonx'] = '';
        }

        if (isset($this->request->post['inn'])) {
            $this->data['inn'] = $this->request->post['inn'];
        } elseif (isset($user_info)) {
            $this->data['inn'] = $user_info['inn'];
        } else {
            $this->data['inn'] = '';
        }
        if (isset($this->request->post['image'])) {
            $this->data['image'] = $this->request->post['image'];
        } elseif (isset($user_info)) {
            $this->data['image'] = $user_info['image'];
        } else {
            $this->data['image'] = '';
        }

        $this->load->model('tool/image');

        if (isset($user_info) && $user_info['image'] && file_exists(DIR_IMAGE . $user_info['image'])) {
            $this->data['preview'] = $this->model_tool_image->resize($user_info['image'], 100, 100);
        } else {
            $this->data['preview'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
        }

        if (isset($this->request->post['image_l'])) {
            $this->data['image_l'] = $this->request->post['image_l'];
        } elseif (isset($user_info)) {
            $this->data['image_l'] = $user_info['image_l'];
        } else {
            $this->data['image_l'] = '';
        }

        if (isset($user_info) && $user_info['image_l'] && file_exists(DIR_IMAGE . $user_info['image_l'])) {
            $this->data['preview_l'] = $this->model_tool_image->resize($user_info['image_l'], 100, 100);
        } else {
            $this->data['preview_l'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
        }

        if (isset($this->request->post['user_group_id'])) {
            $this->data['user_group_id'] = $this->request->post['user_group_id'];
        } elseif (isset($user_info)) {
            $this->data['user_group_id'] = $user_info['user_group_id'];
        } else {
            $this->data['user_group_id'] = '';
        }

        $this->load->model('user/user_group');

        $this->data['user_groups'] = $this->model_user_user_group->getUserGroups();

        if (isset($this->request->post['status'])) {
            $this->data['status'] = $this->request->post['status'];
        } elseif (isset($user_info)) {
            $this->data['status'] = $user_info['status'];
        } else {
            $this->data['status'] = 0;
        }

        if (isset($this->request->post['contract'])) {
            $this->data['contract'] = $this->request->post['contract'];
        } elseif (isset($user_info)) {
            $this->data['contract'] = $user_info['contract'];
        } else {
            $this->data['contract'] = 0;
        }
        //      		if($this->data['contract']=='1'){
        if (isset($this->request->get['user_id'])) {
            $this->data['contract_download'] = HTTPS_SERVER . 'index.php?route=user/user_contract&token=' . $this->session->data['token'] . '&user_id=' . $this->request->get['user_id'] ;
        }
        //      		}

        $this->template = 'user/user_form.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    }

    private function validateForm() {
        if (!$this->user->hasPermission('modify', 'user/user')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if ((strlen(utf8_decode($this->request->post['username'])) < 3) || (strlen(utf8_decode($this->request->post['username'])) > 20)) {
            $this->error['username'] = $this->language->get('error_username');
        }

        if ((strlen(utf8_decode($this->request->post['firstname'])) < 3) || (strlen(utf8_decode($this->request->post['firstname'])) > 32)) {
            $this->error['firstname'] = $this->language->get('error_firstname');
        }

        if ((strlen(utf8_decode($this->request->post['lastname'])) < 3) || (strlen(utf8_decode($this->request->post['lastname'])) > 32)) {
            $this->error['lastname'] = $this->language->get('error_lastname');
        }

        if (($this->request->post['password']) || (!isset($this->request->get['user_id']))) {
            if ((strlen(utf8_decode($this->request->post['password'])) < 4) || (strlen(utf8_decode($this->request->post['password'])) > 20)) {
                $this->error['password'] = $this->language->get('error_password');
            }

            if ($this->request->post['password'] != $this->request->post['confirm']) {
                $this->error['confirm'] = $this->language->get('error_confirm');
            }
        }

        if (!$this->error) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    private function validateDelete() {
        if (!$this->user->hasPermission('modify', 'user/user')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        foreach ($this->request->post['selected'] as $user_id) {
            if ($this->user->getId() == $user_id) {
                $this->error['warning'] = $this->language->get('error_account');
            }
        }

        if (!$this->error) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}

?>